package com.kingyea.mobilepolice.user.service.impl;

import com.google.common.collect.Lists;
import com.kingyea.mobilepolice.role.bean.RolePo;
import com.kingyea.mobilepolice.role.service.RoleService;
import com.kingyea.mobilepolice.security.SecurityUtils;
import com.kingyea.mobilepolice.system.StateCode;
import com.kingyea.mobilepolice.user.bean.User;
import com.kingyea.mobilepolice.user.bean.UserCondition;
import com.kingyea.mobilepolice.user.dao.UserMapper;
import com.kingyea.mobilepolice.user.service.UserService;
import com.kingyea.mobilepolice.util.ExceptionConditions;
import com.kingyea.mobilepolice.util.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Mr.Lin
 * @version v1.0.0
 * @description User 实现类
 * @date Created in 2018-03-30 15:28:59
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserMapper userMapper;

    @Autowired
    private RoleService roleService;

    public static void main(String[] args) {
        System.out.println(RandomUtils.genUUID());
    }

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public User getById(Integer id) throws Exception {
        return this.userMapper.getById(id);
    }

    @Override
    public User getByUserId(String userId) {
        return userMapper.getByUserId(userId);
    }

    @Override
    public User getByUserName(String userName) {
        return userMapper.getByUserName(userName);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<User> search(UserCondition condition) throws Exception {
        List<User> list = userMapper.search(condition);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(String id) throws Exception {
        return userMapper.deleteById(id);
    }

    /**
     * 保存
     *
     * @param user
     * @return
     */
    @Override
    public User save(User user) {
        user.setUserId(RandomUtils.genUUID());
//		user.setOrgSerialNo(RandomUtils.genUUID());
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setCreatedBy(SecurityUtils.getCurrentUser().getUserId());
        ExceptionConditions.checkArgument(userMapper.save(user) == 1, StateCode.SYSTEM_ERROR);
        if (user.getRole().equals("")) {
            return user;
        }
        RolePo po = new RolePo();
        String[] list = user.getRole().split(",");
        po.setUserId(user.getUserId());
        if (list.length > 0) {
            //添加角色
            po.setRoleIds(Lists.newArrayList(list).stream().map(Integer::valueOf).collect(Collectors.toList()));
            ExceptionConditions.checkArgument(roleService.addRolesForUserId(po), StateCode.SYSTEM_ERROR);
        }
        return user;
    }

    /**
     * 更新
     *
     * @param user
     * @return
     */
    @Override
    public User update(User user) throws Exception {
        userMapper.update(user);
        RolePo po = new RolePo();
        po.setUserId(user.getUserId());
        po.setRoleIds(new ArrayList<>());
        roleService.delRolesForUserId(po); // 删除角色关联
        if (user.getRole().equals("")) {
            return user;
        }
        String[] list = user.getRole().split(",");
        if (list.length > 0) {
            //添加角色
            po.setRoleIds(Lists.newArrayList(list).stream().map(Integer::valueOf).collect(Collectors.toList()));
            roleService.addRolesForUserId(po);
        }
        return user;
    }

    @Override
    public List<User> list(Map<String, Object> map) {
        LOGGER.info("test log");
        return userMapper.listByOrgId(map);
    }
}
